package com.pactera.base.config;

import com.pactera.base.config.properties.SwaggerSiteProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author wangrui
 * swagger配置
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig implements OperationBuilderPlugin {

    @Autowired
    private SwaggerSiteProperties swaggerSiteProperties;

    private String host() {
        String osName = System.getProperty("os.name");
        return osName.startsWith("Mac") || osName.startsWith("Window") ? null : swaggerSiteProperties.getHost();
    }

    @Bean
    public Docket test0() {
        return new Docket(DocumentationType.SWAGGER_2)
                .host(this.host())
                .apiInfo(this.apiInfo())
                .select()
                // 扫描路径
                .apis(RequestHandlerSelectors.basePackage("com.pacter"))
                // 正则匹配请求路径，并分配至当前分组，当前所有接口
                .paths(PathSelectors.any())
                .build();
                // 分组名称
                //.groupName();
    }

    @Override
    public void apply(OperationContext operationContext) {

    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(swaggerSiteProperties.getExplanation() + " API 接口文档")
                .description(swaggerSiteProperties.getDescription())
                .version(swaggerSiteProperties.getVersion())
                .termsOfServiceUrl(swaggerSiteProperties.getHost())
                .contact(new Contact("中电文思海辉", "中电文思海辉's phone", "中电文思海辉's email"))
                .build();
    }
}
